package com.fernfx.xingtan.my.presenter;

import android.os.CountDownTimer;
import android.widget.TextView;

import com.fernfx.xingtan.common.base.BaseEntity;
import com.fernfx.xingtan.common.base.BaseIRequestCallback;
import com.fernfx.xingtan.common.base.BaseView;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.my.contract.MyBankCardsContract;
import com.fernfx.xingtan.my.model.MyBankCardsModel;
import com.fernfx.xingtan.utils.FastJsonUtil;

import java.util.Map;

/**
 * Created by admin on 2018/8/17.
 */

public class MyBankCardsPresenter<P extends MyBankCardsContract.View> extends BaseIRequestCallback implements MyBankCardsContract.Presenter {
    private P P;
    private MyBankCardsContract.Model model;

    @Override
    public void init(BaseView view) {
        this.P = (P)view;
        this.model = new MyBankCardsModel();
    }

    @Override
    public void request(Map<String, Object> requestArgsMap) {
        model.request(requestArgsMap,this);
    }

    @Override
    public CountDownTimer getTimeCount(final TextView clickTv) {
        return new CountDownTimer(60000,1000) {
            private boolean flag = true;

            @Override
            public void onTick(long millisUntilFinished) {
                if (flag) {
                    clickTv.setClickable(false);
                    flag = false;
                }
                clickTv.setText(millisUntilFinished /1000+"秒");
            }

            @Override
            public void onFinish() {
                clickTv.setText("重新发送");
                clickTv.setClickable(true);
            }
        };
    }

    @Override
    public void onSuccessful(NetworkOKResult result) {
        P.getActivity().hideLoading();
        String message = result.getMessage();
        BaseEntity baseEntity = FastJsonUtil.fromBean(message, BaseEntity.class);

    }

    @Override
    public void detachView() {
        if (model != null) {
            model = null;
        }
    }
}
